package com.shanghai.market.plshnaghai.login;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.shanghai.market.plshnaghai.R;
import com.shanghai.market.plshnaghai.abstracts.AbstractModelGet;
import com.shanghai.market.plshnaghai.interfaces.IObjectView;
import com.shanghai.market.plshnaghai.interfaces.IStringView;
import com.shanghai.market.plshnaghai.model.login.CodeModel;
import com.shanghai.market.plshnaghai.publics.BaseActivity;
import com.shanghai.market.plshnaghai.publics.CountDownTimerUtils;

/**
 * 手机验证
 */

public class PhoneVerifyActivity extends BaseActivity implements View.OnClickListener, IObjectView<String>, IStringView<String> {

    private TextView mButtonTime;  //验证码
    private TextView mImageViewLogin;   //登录
    private CountDownTimerUtils mCountDownTimerUtils;  //倒计时
    private TextView mRegister;    // 立即注册
    private EditText mEditTextPhone;  //手机号码
    private EditText mEditTextNumber;  //验证码
    private AbstractModelGet modelcode;
    //    private AbstractModelGet modelphone;
    private String mAuthCode;
    private String phone;

    @Override
    public void initParms() {

    }

    @Override
    public View bindView() {
        View view = LayoutInflater.from(this).inflate(R.layout.activity_phone_registered, null);
        return view;
    }

    @Override
    public int bindLayout() {
        return 0;
    }

    @Override
    public void initView(View view) {
        mButtonTime = (TextView) view.findViewById(R.id.button_time);
        mImageViewLogin = (TextView) view.findViewById(R.id.login);
        mRegister = (TextView) view.findViewById(R.id.register);
        mEditTextPhone = (EditText) view.findViewById(R.id.editText_phone);
        mEditTextNumber = (EditText) view.findViewById(R.id.editText_number);
    }

    @Override
    public void setListener() {
        mButtonTime.setOnClickListener(this);
        mRegister.setOnClickListener(this);
        mImageViewLogin.setOnClickListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        mRegister.setVisibility(View.GONE);
        mImageViewLogin.setText("提交");
        mBarTitle.setText("手机验证");
        mButtonTime.setText("获取验证码");
        modelcode = new CodeModel(PhoneVerifyActivity.this);
//        modelphone = new PhoneCodeModel(PhoneVerifyActivity.this);
        mCountDownTimerUtils = new CountDownTimerUtils(mButtonTime, 60000, 1000);


    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.button_time:
                mButtonTime.setBackgroundResource(R.mipmap.time_button);
                mCountDownTimerUtils.start();
                code();
                showToast("button_time");
                break;
            case R.id.register:
//                intent = new Intent(this, MaterialWriteActivity.class);
//                startActivity(intent);
//                showToast("register");
                break;
            case R.id.login:
                //提交
                login();

                showToast("register");
                break;
            default:
                break;
        }
    }

    private void login() {
        String code = mEditTextNumber.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showToast("手机号码不能为空");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showToast("验证码不能为空");
            return;
        }
        if (!code.equals(mAuthCode)) {
            showToast("验证码输入不对");
            return;
        }
//        String str = "type=2" + "&mobile=" + phone + "&code=" + code + "&";
//        VolleyParams params = new VolleyParams();
//        params.put("mobile", phone);
//        params.put("code", code);
//        modelphone.setParams(params);
//        modelphone.getData();
        Intent intent = new Intent(this, MaterialWriteActivity.class);
        intent.putExtra("phone", phone);
        startActivity(intent);
    }

    private void code() {
        phone = mEditTextPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showToast("手机号码不能为空");
            return;
        }
        String str = "type=1" + "&mobile=" + phone + "&";
        modelcode.getData(new String[]{str});

    }

    @Override
    public void failure(String message) {
        showToast(message);
    }

    @Override
    public void success(String s) {
        if (!TextUtils.isEmpty(s)) {
            mAuthCode = s;
            showToast(s);

        }
    }

    //占时不用
    @Override
    public void failureString(String message) {

    }

    @Override
    public void successString(String t) {
        if (!TextUtils.isEmpty(t)) {


        }

    }
}
